/*
  * Mirante Tecnologia
  * Copyright 2010, Mirante Informatica LTDA, 
  * and individual contributors as indicated by the @authors tag
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
package com.miranteinfo.seam.util;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.miranteinfo.seam.hibernate.BaseEntity;

/**
 * Classe suporte para todos os DTOs. Define uma serie de atributos e metodos para tornar o desenvolvimento
 * mais facil.
 * 
 * @author lucas lins
 *
 * @param <T>
 * @param <PK>
 */
public abstract class DtoSupport<T extends BaseEntity, PK extends Serializable> implements Serializable{

	private static final long serialVersionUID = 1L;

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// ATTRIBUTES
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	protected T entity;
	
	protected boolean checked;
	
	protected Map<Object, Object> attributes;
	
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// CONSTRUCTOR
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	public DtoSupport(T entity){
		if(entity == null){
			throw new RuntimeException("DtoSupport: Entity must be not null");
		}
		this.entity = entity;
		this.checked = false;
		this.attributes = new HashMap<Object, Object>();
	}
	
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// PUBLIC METHODS
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	/**
	 * Retorna o id da entidade vinculada ao dto.
	 * @return id
	 */
	@SuppressWarnings("unchecked")
	public final PK getId(){
		return (PK) entity.getId();
	}
	
	/**
	 * Insere um atributo no mapa de atributos.
	 * @param key
	 * @param value
	 */
	public void addAttribute(Object key, Object value){
		attributes.put(key, value);
	}
	
	/**
	 * Verifica se existe um atributo para a chave fornecida.
	 * @param key
	 * @return boolean
	 */
	public boolean attributesContainsKey(Object key){
		return attributes.containsKey(key);
	}
	
	/**
	 * Verifica se o valor passado esta no mapa de atributos
	 * @param value
	 * @return boolean
	 */
	public boolean attributesContainsValue(Object value){
		return attributes.containsValue(value);
	}
	
	/**
	 * retorna o atributo que tem como chave a informada por parametro.
	 * @param <AT>
	 * @param key
	 * @param attributeType
	 * @return atributo
	 */
	@SuppressWarnings({"unchecked"})
	public <AT> AT getAttribute(Object key, Class<AT> attributeType){ 
		return (AT) attributes.get(key);
	}
	
	/**
	 * Retorna a lista de atributos.
	 * @return lista
	 */
	public Collection<Object> getAttributesValues(){
		return attributes.values();
	}
	
	/**
	 * Retorna o set das chaves dos atributos.
	 * @return set
	 */
	public Set<Object> getAttributesKeys(){
		return attributes.keySet();
	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// GETTERS AND SETTERS
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	public T getEntity() {
		return entity;
	}

	public void setEntity(T entity) {
		this.entity = entity;
	}

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}

	public void setAttributes(Map<Object, Object> attributes) {
		this.attributes = attributes;
	}

	public Map<Object, Object> getAttributes() {
		return attributes;
	}
	
}
